<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
      <el-tabs value="first">
        <el-tab-pane label="通用信息" name="first">
          <el-form-item label="商品类型" prop="categoryId">
            <el-input disabled v-model="dataForm.categoryName" placeholder="商品类型">
              <el-button slot="append" icon="el-icon-view" @click="dialogVisible = true"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item label="商品序列号" prop="goodsSn">
            <el-input v-model="dataForm.goodsSn" placeholder="商品序列号"></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="名称"></el-input>
          </el-form-item>
          <el-form-item label="品牌" prop="brandId">
            <el-select v-model="dataForm.brandId" placeholder="请选择">
              <el-option
                v-for="item in brands"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="类别" prop="attributeCategory">
            <el-select v-model="dataForm.attributeCategory" placeholder="请选择">
              <el-option
                v-for="item in attributeCategories"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品库存" prop="goodsNumber">
            <el-input-number v-model="dataForm.goodsNumber" controls-position="right" :min="1"></el-input-number>
          </el-form-item>
          <el-form-item label="零售价格" prop="retailPrice">
            <el-input-number v-model="dataForm.retailPrice" controls-position="right" :min="1"></el-input-number>
          </el-form-item>
          <el-form-item label="市场价" prop="marketPrice">
            <el-input-number v-model="dataForm.marketPrice" controls-position="right" :min="1"></el-input-number>
          </el-form-item>
          <el-form-item label="商品主图" prop="primaryPicUrl">
            <el-input v-model="dataForm.primaryPicUrl" placeholder="商品主图"></el-input>
          </el-form-item>
          <el-form-item label="商品列表图" prop="listPicUrl">
            <el-input v-model="dataForm.listPicUrl" placeholder="商品列表图"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="详细描述" name="second">

        </el-tab-pane>
        <el-tab-pane label="其他信息" name="third">
          <el-form-item label="排序" prop="sortOrder">
            <el-input-number v-model="dataForm.sortOrder" controls-position="right" :min="1"></el-input-number>
          </el-form-item>
          <el-form-item label="上架" prop="isOnSale">
              <el-radio v-model="dataForm.isOnSale" label="1">是</el-radio>
              <el-radio v-model="dataForm.isOnSale" label="0">否</el-radio>
          </el-form-item>
          <el-form-item label="是否新商品" prop="isNew">
            <el-radio v-model="dataForm.isNew" label="1">是</el-radio>
            <el-radio v-model="dataForm.isNew" label="0">否</el-radio>
          </el-form-item>
          <el-form-item label="热销" prop="isHot">
            <el-radio v-model="dataForm.isHot" label="1">是</el-radio>
            <el-radio v-model="dataForm.isHot" label="0">否</el-radio>
          </el-form-item>
          <el-form-item label="推广描述" prop="promotionDesc">
            <el-input v-model="dataForm.promotionDesc" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="关键字" prop="keywords">
            <el-input v-model="dataForm.keywords" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="简明介绍" prop="goodsBrief">
            <el-input v-model="dataForm.goodsBrief" placeholder=""></el-input>
          </el-form-item>
        </el-tab-pane>
      </el-tabs>
<!--    <el-form-item label="" prop="goodsDesc">-->
<!--      <el-input v-model="dataForm.goodsDesc" placeholder=""></el-input>-->
<!--    </el-form-item>-->
    </el-form>
    <el-dialog
      width="300px"
      height="450px"
      title="选择类型"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      append-to-body>
      <el-tree
        ref="tree"
        :data="treeData"
        :props="defaultProps"
        accordion
        @node-click="handleNodeClick">
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确  定</el-button>
      </div>
    </el-dialog>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dialogVisible: false,
        dataForm: {
          id: 0,
          categoryId: '',
          goodsSn: '',
          name: '',
          brandId: '',
          goodsNumber: '',
          keywords: '',
          goodsBrief: '',
          goodsDesc: '',
          isOnSale: '1',
          addTime: '',
          sortOrder: '',
          isDelete: '',
          attributeCategory: '',
          counterPrice: '',
          extraPrice: '',
          isNew: '1',
          goodsUnit: '',
          primaryPicUrl: '',
          listPicUrl: '',
          retailPrice: '',
          sellVolume: '',
          primaryProductId: '',
          unitPrice: '',
          promotionDesc: '',
          promotionTag: '',
          appExclusivePrice: '',
          isAppExclusive: '',
          isLimited: '',
          isHot: '0',
          marketPrice: '',
          createUserId: '',
          updateUserId: '',
          updateTime: '',
          createUserDeptId: '',
          categoryName: ''
        },
        brands: [], // 品牌
        macros: [], // 商品单位
        attributeCategories: [], // 属性类别
        treeData: '',
        defaultProps: {
          children: 'children',
          label: 'name' // 这里的名字要和你封装的数据中的节点的名字一样
        },
        dataRule: {
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.getCategory()
        this.getBrands()
        this.getAttributeCategories()
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/platform/goods/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.categoryId = data.data.categoryId
                this.dataForm.goodsSn = data.data.goodsSn
                this.dataForm.name = data.data.name
                this.dataForm.brandId = data.data.brandId
                this.dataForm.goodsNumber = data.data.goodsNumber
                this.dataForm.keywords = data.data.keywords
                this.dataForm.goodsBrief = data.data.goodsBrief
                this.dataForm.goodsDesc = data.data.goodsDesc
                this.dataForm.isOnSale = data.data.isOnSale + ''
                this.dataForm.addTime = data.data.addTime
                this.dataForm.sortOrder = data.data.sortOrder
                this.dataForm.isDelete = data.data.isDelete
                this.dataForm.attributeCategory = data.data.attributeCategory
                this.dataForm.counterPrice = data.data.counterPrice
                this.dataForm.extraPrice = data.data.extraPrice
                this.dataForm.isNew = data.data.isNew + ''
                this.dataForm.goodsUnit = data.data.goodsUnit
                this.dataForm.primaryPicUrl = data.data.primaryPicUrl
                this.dataForm.listPicUrl = data.data.listPicUrl
                this.dataForm.retailPrice = data.data.retailPrice
                this.dataForm.sellVolume = data.data.sellVolume
                this.dataForm.primaryProductId = data.data.primaryProductId
                this.dataForm.unitPrice = data.data.unitPrice
                this.dataForm.promotionDesc = data.data.promotionDesc
                this.dataForm.promotionTag = data.data.promotionTag
                this.dataForm.appExclusivePrice = data.data.appExclusivePrice
                this.dataForm.isAppExclusive = data.data.isAppExclusive
                this.dataForm.isLimited = data.data.isLimited
                this.dataForm.isHot = data.data.isHot + ''
                this.dataForm.marketPrice = data.data.marketPrice
                this.dataForm.createUserId = data.data.createUserId
                this.dataForm.updateUserId = data.data.updateUserId
                this.dataForm.updateTime = data.data.updateTime
                this.dataForm.createUserDeptId = data.data.createUserDeptId
                this.dataForm.categoryName = data.data.categoryName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/platform/goods/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'categoryId': this.dataForm.categoryId,
                'goodsSn': this.dataForm.goodsSn,
                'name': this.dataForm.name,
                'brandId': this.dataForm.brandId,
                'goodsNumber': this.dataForm.goodsNumber,
                'keywords': this.dataForm.keywords,
                'goodsBrief': this.dataForm.goodsBrief,
                'goodsDesc': this.dataForm.goodsDesc,
                'isOnSale': this.dataForm.isOnSale,
                'addTime': this.dataForm.addTime,
                'sortOrder': this.dataForm.sortOrder,
                'isDelete': this.dataForm.isDelete,
                'attributeCategory': this.dataForm.attributeCategory,
                'counterPrice': this.dataForm.counterPrice,
                'extraPrice': this.dataForm.extraPrice,
                'isNew': this.dataForm.isNew,
                'goodsUnit': this.dataForm.goodsUnit,
                'primaryPicUrl': this.dataForm.primaryPicUrl,
                'listPicUrl': this.dataForm.listPicUrl,
                'retailPrice': this.dataForm.retailPrice,
                'sellVolume': this.dataForm.sellVolume,
                'primaryProductId': this.dataForm.primaryProductId,
                'unitPrice': this.dataForm.unitPrice,
                'promotionDesc': this.dataForm.promotionDesc,
                'promotionTag': this.dataForm.promotionTag,
                'appExclusivePrice': this.dataForm.appExclusivePrice,
                'isAppExclusive': this.dataForm.isAppExclusive,
                'isLimited': this.dataForm.isLimited,
                'isHot': this.dataForm.isHot,
                'marketPrice': this.dataForm.marketPrice,
                'createUserId': this.dataForm.createUserId,
                'updateUserId': this.dataForm.updateUserId,
                'updateTime': this.dataForm.updateTime,
                'createUserDeptId': this.dataForm.createUserDeptId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 点击事件
      handleNodeClick (data) {
        this.dataForm.categoryName = data.name
        this.dataForm.categoryId = data.id
      },
      getCategory () {
        this.dataForm.categoryName = ''
        this.$http({
          url: this.$http.adornUrl('/platform/category/queryAll'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.treeData = data.data
        })
      },
      getBrands () {
        this.$http({
          url: this.$http.adornUrl('/platform/brand/queryAll'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.brands = data.data
        })
      },
      getAttributeCategories () {
        this.$http({
          url: this.$http.adornUrl('/platform/attributecategory/queryAll'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.attributeCategories = data.data
        })
      }
    }
  }
</script>
